package com.wyj.longrangeclient.Service;

import android.app.Service;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.IBinder;

import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSON;
import com.wyj.longrangeclient.Bean.ActionBean;
import com.wyj.longrangeclient.Util.RxBus;
import com.wyj.longrangeclient.Util.WebSocketUtil;

import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import okio.ByteString;
import rx.Observer;

/**
 * author : 泥菩萨
 * time   : 2023/04/19
 */
public class WebSocketService extends Service {

   @Nullable
   @Override
   public IBinder onBind(Intent intent) {
      return null;
   }

   @Override
   public void onCreate() {
      super.onCreate();

      RxBus.getInstance().Register("bitmap", new Observer<Object>() {

         @Override
         public void onCompleted() {

         }

         @Override
         public void onError(Throwable e) {

         }

         @Override
         public void onNext(Object o) {

            try {

               sendByte((Bitmap) o);

            }catch (Exception e){

            }

         }
      });

   }

   @Override
   public int onStartCommand(Intent intent, int flags, int startId) {

      WebSocketUtil.getWebSocketUtil().connect(intent.getStringExtra("url"));

      WebSocketUtil.getWebSocketUtil().setmCallback(new WebSocketUtil.Callback() {
         @Override
         public void getData(byte[] data) {

         }

         @Override
         public void getData(String data) {

            try {

               handle(data);

            }catch (Exception e){

            }
         }

         @Override
         public void open() {

         }

         @Override
         public void end() {

         }

         @Override
         public void msg(String data) {

            RxBus.getInstance().post("SocketMsg",data);

            if ("当前正在运行".equals(data)){

               RxBus.getInstance().post("AccessibilityService",data);
               RxBus.getInstance().post("ScreenRecorder",data);
               RxBus.getInstance().post("ServiceMsg","网络服务:正在运行");
            }
         }

      });

      return super.onStartCommand(intent, flags, startId);
   }

   private void handle(String data){

      ActionBean mActionBean = JSON.parseObject(data,ActionBean.class);

      if (mActionBean.getAppAction().equals("dj")){

         float x = mActionBean.getX1()*getResources().getDisplayMetrics().widthPixels;
         float y = mActionBean.getY1()*getResources().getDisplayMetrics().heightPixels;

         mActionBean.setX1(x);
         mActionBean.setY1(y);

      }else if (mActionBean.getAppAction().equals("ca")){

         float x = mActionBean.getX1()*getResources().getDisplayMetrics().widthPixels;
         float y = mActionBean.getY1()*getResources().getDisplayMetrics().heightPixels;

         mActionBean.setX1(x);
         mActionBean.setY1(y);

      }else if (mActionBean.getAppAction().equals("hd")){

         float x1 = mActionBean.getX1()*getResources().getDisplayMetrics().widthPixels;
         float y1 = mActionBean.getY1()*getResources().getDisplayMetrics().heightPixels;
         float x2 = mActionBean.getX2()*getResources().getDisplayMetrics().widthPixels;
         float y2 = mActionBean.getY2()*getResources().getDisplayMetrics().heightPixels;

         mActionBean.setX1(x1);
         mActionBean.setY1(y1);
         mActionBean.setX2(x2);
         mActionBean.setY2(y2);
      }

      RxBus.getInstance().post("YourAccessibilityService",mActionBean);
   }

   private void sendByte(Bitmap bitmap){

      byte[] byteArray;
      int index = 0;
      int byteMax = 1024;

      ByteArrayOutputStream stream = new ByteArrayOutputStream();
      bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
      byteArray = stream.toByteArray();


      while (index<byteArray.length){

         byte[] fp;
         int stop = 0;
         int length = index + byteMax;

         if (length>byteArray.length){

            length=byteArray.length;
            fp = new byte[byteArray.length % byteMax];

         }else {

            fp = new byte[byteMax];
         }

         for (int i = index;i<length;i++){

            fp[stop] = byteArray[i];
            stop = stop+1;
         }

         if (fp.length>0){

            WebSocketUtil.getWebSocketUtil().send(ByteString.of(ByteBuffer.wrap(fp,0,fp.length)));
         }

         index = length;
      }

   }

   @Override
   public void onDestroy() {
      super.onDestroy();

      WebSocketUtil.getWebSocketUtil().cancel();
      RxBus.getInstance().Cancellation("bitmap");
      RxBus.getInstance().post("ServiceMsg","网络服务:关闭");
   }
}
